package me.zhengjie.modules.enrstu.service.impl;

import lombok.RequiredArgsConstructor;

import me.zhengjie.modules.enrstu.domain.FileInformation;
import me.zhengjie.modules.enrstu.domain.ReviewInformationLog;
import me.zhengjie.modules.enrstu.service.*;
import me.zhengjie.modules.enrstu.service.dto.*;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Security;
import java.util.*;

@Service
@RequiredArgsConstructor
public class AuditServiceImpl implements AuditService {
    private final ChildrenInformationService childrenInformationService;

    private final FileInformationService fileInformationService;
    private final GuardianChildrenService guardianChildrenService;
    private final ReviewInformationService reviewInformationService;
    private final ReviewInformationLogService reviewLogService;
    private final SchoolDistrictService schDiService;
    private final SchoolService schService;
    private final UserRepository userRepository;

    @Override
    public PageResult<ReviewWaitInfoListDto> findReviewWaitInfoList(ReviewWaitInfoQueryCriteria resources, Pageable pageable){
        return reviewInformationService.findReviewWaitInfoList(resources, pageable);
    }

    @Override
    public PageResult<ReviewAlreadyOrAcceptedInfoListDto> findReviewAlreadyInfoList(ReviewAlreadyOrAcceptedInfoQueryCriteria resources, Pageable pageable) {
        return reviewInformationService.findReviewAlreadyInfoList(resources,pageable);
    }

    @Override
    public PageResult<ReviewAlreadyOrAcceptedInfoListDto> findReviewAcceptedInfoList(ReviewAlreadyOrAcceptedInfoQueryCriteria resources, Pageable pageable) {
        return reviewInformationService.findReviewAcceptedInfoList(resources,pageable);
    }
    @Override
    public List<Map<String, Object>> getListDetails(String id) {
        List<Map<String,Object>> returnList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        //1.获取儿童信息
        ChildrenInformationDto childrenInfoBy = childrenInformationService.findById(id);
        //2.获取监护人信息
        List<GuardianInformationDto> guardianInformation = guardianChildrenService.findGuardianChildrenById(id);
        //3.获取附件信息
        List<String> linkIds = new ArrayList<>();
        linkIds.add(childrenInfoBy.getId());
        guardianInformation.forEach(guardianInformationDto -> linkIds.add(guardianInformationDto.getId()));
        List<FileInformation> maps = fileInformationService.fileInfo(linkIds);
        //4.获取审核明细
        List<ReviewInformationLog> reviewInfoLog = reviewLogService.findByChildId(id);
        //5.获取儿童所在学区
        SchoolDto schoolDto = schService.findById(childrenInfoBy.getSchoolId());
        SchoolDistrictDto schoolDistrictDto = schDiService.findById(schoolDto.getDistrictId());
        //5.返回数据
        map.put("chiInfo", childrenInfoBy);
        map.put("guardsInfo", guardianInformation);
        map.put("filesInfo", maps);
        map.put("rwLogInfo", reviewInfoLog);
        map.put("schoolDto", schoolDistrictDto);
        returnList.add(map);
        return returnList;
    }
    @Override
    public byte[] fileDownload(List<String> linkIds) {
        return fileInformationService.fileDownload(linkIds);
    }


    @Override
    public void review(ReviewOrAcceptedHandler reviewHandler) {
        reviewInformationService.review(reviewHandler);
    }

    @Override
    public void accepted(List<ReviewOrAcceptedHandler> acceptedHandler) {
        reviewInformationService.accepted(acceptedHandler);
    }

    @Override
    public void download(HttpServletResponse response) throws IOException {

        reviewInformationService.download(response);
    }

    @Override
    public User getUserInfo() {
        User user = userRepository.findById(SecurityUtils.getCurrentUserId()).orElseGet(User::new);
        return userRepository.findById(SecurityUtils.getCurrentUserId()).orElseGet(User::new);
    }

}
